package com.lemner.motion.base;

import android.support.annotation.NonNull;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 小博 on 2018/3/23.
 */

public  class BaseModel<T> {
    public BaseService service;

    public BaseModel() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(URL.BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
        service = retrofit.create(BaseService.class);
    }

    public void callEnqueue(Call<T> call, final BaseListener<T> listener) {
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
                if (response.body() != null && response.isSuccessful())
                    listener.onResponse(response.body());
                else
                    listener.onFail(response.message()+response.code());
            }

            @Override
            public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
                listener.onFail(t.getMessage());
            }
        });
    }
}
